/**
 * @title FilterAdapter.java
 * @package com.esun.cheetah.view.match.matchlist.adapter
 * @description 过滤时保存联赛名的adapter
 * @author liugan
 * @date 2012-4-5
 * @copyright 深圳市易讯天空
 */
package com.esun.cheetah.view.match.matchlist.adapter;

import java.util.HashSet;
import java.util.Set;

import com.esun.cheetah.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

/**
 * @title FilterAdapter
 * @description 用于过滤的联赛数据
 * @author liugan
 */
public class FilterAdapter extends BaseAdapter {

	private Context context;
	private String[] allLeagues;
	private HashSet<String> selectedLeagues = new HashSet<String>();

	public FilterAdapter(Context context) {
		this.context = context;
	}

	public void setAllLeagues(String[] allLeagues) {
		this.allLeagues = allLeagues;
	}

	public void setSelectedLeagues(Set<String> selectedLeagues) {
		this.selectedLeagues.clear();
		this.selectedLeagues.addAll(selectedLeagues);
	}

	public HashSet<String> getSelectedLeagues() {
		return selectedLeagues;
	}

	@Override
	public int getCount() {
		if (allLeagues == null) {
			return 0;
		}
		return allLeagues.length;
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		CheckBox cbLeague = (CheckBox) LayoutInflater.from(context).inflate(
				R.layout.filter_match_item, null);
		// 设置联赛名称
		cbLeague.setText(allLeagues[position]);
		// 设置联赛是否被选中
		if (selectedLeagues.contains(allLeagues[position])) {
			cbLeague.setChecked(true);
		}
		// 设置联赛CheckBox的监听器
		cbLeague.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					selectedLeagues.add(buttonView.getText().toString());
				} else {
					selectedLeagues.remove((buttonView.getText().toString()));
				}
			}
		});
		return cbLeague;
	}

	public void selectAll() {
		selectedLeagues.clear();
		for (String league : allLeagues) {
			selectedLeagues.add(league);
		}
	}

	public void selectInverse() {
		for (String league : allLeagues) {
			if (selectedLeagues.contains(league)) {
				selectedLeagues.remove(league);
			} else {
				selectedLeagues.add(league);
			}
		}
	}
}
